package cn.thread.pool.dynamic.thread.pool.sdk.config;

import cn.thread.pool.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.codec.Kryo5Codec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @author: xiaoxie
 * create: 2025-02-12 22:03
 * @BelongsProject: custom-dynamic-thread-pool
 * @BelongsPackage: cn.thread.pool.dynamic.thread.pool.sdk.config
 * description: redisson配置
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "redis.sdk.config")
public class redissonConfig {
    private String host;

    private String password;

    private String port;

    private int timeout = 3000;
    private static String ADDRESS_PREFIX = "redis://";

    /**
     * 自动装配
     *
     */
    @Bean
    RedissonClient redissonSingle() {

        Config config = new Config();
        config.setCodec(JsonJacksonCodec.INSTANCE);

        if(!StringUtils.hasText(host)){
            throw new RuntimeException("host is  empty");
        }
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(ADDRESS_PREFIX + this.host + ":" + port)
                .setTimeout(this.timeout);
        if(StringUtils.hasText(this.password)) {
            serverConfig.setPassword(this.password);
        }
        return Redisson.create(config);
    }
}
